function read_mykad(url, data) {
    var myAjax = new Ajax.Request(
	url,
	{method: 'post', parameters: data, onComplete: read_mykad_response_ku}
    );
}

function read_mykad_response(originalRequest) {
	a = originalRequest.responseText
		//alert(a)
		if(a == 'Card.ERROR' || a=='SCardConnect: Error 80100065') alert('Card. Error')
		qq = a.split(';')
		//alert(qq)
		if(qq[0] == 'T'){
			zzz = window.location + ''
			yyy = zzz.split("?")
			rightside = yyy[1].split("&")
			gact = rightside[0].split("=")
			window.location = "existing_visitor.php?act=" + gact[1] + "&ic=" + qq[1]
		}else{
		jpn_data = a.split(';')
			for(i=0 ; i < jpn_data.length; i++){
					b = jpn_data[i]
					c = b.split('=')
			field_name = c[0]
			field_value  = c[1]
			if(getid = document.getElementById(field_name))
						getid.innerHTML=field_value
			}
		if(jpn_data[1]){
			ic_arr = jpn_data[1].split('=')
				if(foto = ic_arr[1]){
					document.getElementById('foto').src="./images/" + foto +".jpg"
					document.getElementById('fotot').src="./images/mykad.jpg"
					document.getElementById('frm_reg').style.visibility = "visible"
					document.getElementById('frm_ic_new').value = ic_arr[1]
				}
		}
	}

}

function read_mykad_response_ku(originalRequest) {
	a = originalRequest.responseText
		//alert(a)
		if(a == 'Card.ERROR' || a=='SCardConnect: Error 80100065') {
			//alert('Card. Error');
			if (confirm('Error reading MyKad. \nClick OK to retry or \nClick Cancel to enter data manually.')) {
				window.location.reload();
			}
			else {
				window.location = "otamu.php?act=checkin_manual";
			}
		}
		qq = a.split(';')
		//alert(qq)
		if(qq[0] == 'T'){
			zzz = window.location + ''
			yyy = zzz.split("?")
			rightside = yyy[1].split("&")
			gact = rightside[0].split("=")
			window.location = "otamu.php?act=checkin_mykad_exist&ic_new=" + qq[1]
		}else{
			//alert('ic takde rekod lagi');
			jpn_data = a.split(';')
			for(i=0 ; i < jpn_data.length; i++){
				b = jpn_data[i]
				c = b.split('=')
				field_name = c[0]
				field_value  = c[1]
				if(getid = document.getElementById(field_name))
					//getid.innerHTML=field_value
					getid.value=field_value
				}
			document.getElementById('indicator').innerHTML = '';
			document.getElementById('indicator').style.height = '1px';

			//checkAppointment();

			if(jpn_data[1]){
				ic_arr = jpn_data[1].split('=')
				if(foto = ic_arr[1]){
					document.getElementById('photo').style.visibility="visible"
					document.getElementById('photo').src="./photos/" + foto +".jpg"
					//document.getElementById('fotot').src="./images/mykad.jpg"
					//document.getElementById('frm_reg').style.visibility = "visible"
					//document.getElementById('frm_ic_new').value = ic_arr[1]
					//document.getElementById('ic_new').value = ic_arr[1]
				}
			}
		}

}


function read_mifare(url, data) {
    var myAjax = new Ajax.Request(
	url,
	{method: 'post', parameters: data, onComplete: read_mifare_response}
    );
}

function read_mifare_response(originalRequest) {
	a = originalRequest.responseText
	//jpn_data = a.split(';')
	if(a.length!=8) {
		//read_mykad('readmykad.php?k=1', '')
		alert("Invalid Card")

	}
	//alert(a)
}

